//
//  WBStatusCell.swift
//  XLWB
//
//  Created by MacBook on 16/7/5.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit
@objc protocol WBStatusCellDelegate: NSObjectProtocol {
    
    //微博Cell选中的URL字符串
    @objc optional func statusCellDidSelectedURLString(cell: WBStatusCell, urlString: String)
}

class WBStatusCell: UITableViewCell {
    
    //代理属性
    weak var delegate: WBStatusCellDelegate?

    
    //微博视图模型
    var viewModel:WBStatusViewModel? {
    
        didSet {
        
            //微博文本
//            statusLabel?.text = viewModel?.status.text
            statusLabel.attributedText = viewModel?.statusAttrText

            // 设置被转发微博的文字
            retweetedLabel?.attributedText = viewModel?.retweetedAttrText

            //姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            
            //会员图标  直接获取属性,不需要计算
            mennberIconView.image = viewModel?.menberIcon
        
            //认证图标
            vipIconView.image = viewModel?.vipIocn
            
            //用户头像
            iconView.da_setImage(urlString: viewModel?.status.user?.profile_image_url,
                placeholderImage: UIImage(named: "avatar_default_big"),
                isAvatar: true)

            //底部工具栏
            ToolBar.viewModel = viewModel
        
            //测试修改配图视图的高度

            //配图视图模型
            pictureView.viewModel = viewModel
            
            // 测试 4 张图像
            //            if viewModel?.status.pic_urls?.count > 4 {
            //                // 修改数组 -> 将末尾的数据全部删除
            //                var picURLs = viewModel!.status.pic_urls!
            //
            //                picURLs.removeSubrange((picURLs.startIndex + 4)..<picURLs.endIndex)
            //
            //                pictureView.urls = picURLs
            //            } else {
            //                pictureView.urls = viewModel?.status.pic_urls
            //            }

            
            
            //设置配图视图的URL 数据
//            pictureView.urls = viewModel?.status.pic_urls

//            pictureView.urls = viewModel?.picURLs
            
            //设置被转发微博的文字
//            retweetedLabel?.text = viewModel?.retweetedText
            
            //设置来源
            print("微博来源 \(viewModel?.status.source)")
            
            sourceLabel.text = viewModel?.status.source
//            sourceLabel.text = viewModel?.sourceStr
        }
    
    }
    
    //头像
    @IBOutlet weak var iconView: UIImageView!
    //姓名
    @IBOutlet weak var nameLabel: UILabel!
    //会员图标
    @IBOutlet weak var mennberIconView: UIImageView!
    //时间
    @IBOutlet weak var timeLabel: UILabel!
    //来源
    @IBOutlet weak var sourceLabel: UILabel!
    //认证
    @IBOutlet weak var vipIconView: UIImageView!
    //正文
    @IBOutlet weak var statusLabel: FFLabel!
    //底部工具栏
    @IBOutlet weak var ToolBar: WBStatusToolBar!
    //配图视图
    @IBOutlet weak var pictureView: WBStatusPictureView!
    
    //被转发微博的标签--原创微博没有此空间,所以用'?'
    @IBOutlet weak var retweetedLabel: FFLabel?
   
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //离屏渲染--异步绘制
        self.layer.drawsAsynchronously = true
        
        //栅格化
        self.layer.shouldRasterize = true
        
        self.layer.rasterizationScale = UIScreen.main().scale
        
        //设置微博文本代理
        statusLabel.delegate = self
        retweetedLabel?.delegate = self
        
    }
}

extension WBStatusCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        // 判断是否是 URL
        if !text.hasPrefix("http://") {
            return
        }
        
        delegate?.statusCellDidSelectedURLString?(cell: self, urlString: text)
    }
}
